import 'dart:core';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/sale/sale_in/following_record/bean/record_config_bean.dart';
import 'package:flutter/material.dart';
import '../../../../r.dart';

typedef MultipleChoiceCallback = void Function(List<int> choices);
typedef SingleChoiceCallback = void Function(int value);
// ignore: must_be_immutable
class WidgetRecordChoice extends StatelessWidget {
  final SingleChoiceCallback singleChoiceCallback;
  final MultipleChoiceCallback  multipleChoiceCallback;
  final int selectIndex;
  final List<int>indexList;
  final ConfigBean configBean;

  List<int>selectIndexList = List<int>();
  WidgetRecordChoice({this.configBean,this.selectIndex,this.singleChoiceCallback,this.multipleChoiceCallback,this.indexList});

  @override
  Widget build(BuildContext context) {
    if(indexList!= null) {
      for(int value in indexList) {
        selectIndexList.add(value);
      }
    }
    return buildRecordSingleChoiceWidget(context);
  }

  Widget buildRecordSingleChoiceWidget(BuildContext context) {
    return Container(
      child: Container(
        color: Colors.white,
        child: Column(
          children: getWidgets(),
        ),
        //padding: const EdgeInsets.only(left: UIConstBean.LRSpace, right: UIConstBean.LRSpace, top: 10),
      ),
    );
  }

  List<Widget>getWidgets(){
    List<Widget> results = List<Widget>();
    results.add(getHeadTitleWidget(configBean.title));
    results.add(DkDivider(DividerOption.horizontal(height: 0.5, indentStart: 16, indentEnd: 0,color: Color(0xffebebeb))));
    if(configBean!= null && configBean.optionalAnswers!= null && configBean.optionalAnswers.length > 0) {
      for(int i = 0; i < configBean.optionalAnswers.length;i++) {
        results.add(buildSingleChoiceWidget(i));
      }
    }
    results.add(Container(
      height: 10,
      color: Color(0xffF7F8F9),
    ));
    return results;
  }

  Widget getHeadTitleWidget(String title) {
    return Container(
      height: 48,
      padding: EdgeInsets.only(left: 16),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          getTitleWidget(title),
          Spacer(),
        ],
      ),
    );
  }

  Widget  getTitleWidget(String title) {
    return RichText(text: TextSpan(
        children: <TextSpan>[
          TextSpan(text:title,
            style: TextStyle(fontSize: 14.0,
              color: Color(0xff666666),fontWeight: FontWeight.normal,
            ),
          ),

          TextSpan(text:singleChoiceCallback!=null ? "（单选）":"（可多选）",
            style: TextStyle(fontSize: 14.0,
              color: Color(0xff999999),fontWeight: FontWeight.normal,
            ),
          ),
        ]));
  }

  bool getSeletIndex(int index) {
    if(singleChoiceCallback!= null) {
      return (selectIndex == index) ? true : false;
    }
    if(multipleChoiceCallback!= null) {
      return selectIndexList.contains(index) ? true : false;
    }
    return false;
  }

  Widget  buildSingleChoiceWidget(int index){
    return GestureDetector(
      child: Container(
        padding: EdgeInsets.only(left: 16,right: 16,top: 17,bottom: 17),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Image.asset( getSeletIndex(index) ?  R.imagesHomeStausFinishPng : R.imagesHomeStausUnfinishPng,width: 19,height: 19,fit:BoxFit.contain,),
            SizedBox(width: 9,),
            Expanded(
              child: Text(configBean.optionalAnswers[index],
                maxLines: 10,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(color: getSeletIndex(index) ?Color(0xff333333) :Color(0xff999999),fontSize: 14,fontWeight: FontWeight.normal),
              ),
            ),
          ],
        ),
      ),
      onTap: (){
        if(singleChoiceCallback!= null) {
          singleChoiceCallback(index);
        }
        if(multipleChoiceCallback!=null){
          if(selectIndexList.contains(index)) {
            selectIndexList.remove(index);
          } else {
            selectIndexList.add(index);
          }
          multipleChoiceCallback(selectIndexList);
        }
      },
    );
  }
}



